﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Core;
using Tesla.Graphics;
using Tesla.Math;

namespace Tesla.Scene.Extension {
    /// <summary>
    /// Controller that controls the scale of the spatial attached to it.
    /// The object will appear to be the same size as the camera draws away
    /// or to the object. Smaller scale distances means a larger scale.
    /// </summary>
    public sealed class ConstantSizeController : ISpatialController {
        private bool _isEnabled;
        private Camera _camera;
        private float _scaleDistance;

        /// <summary>
        /// Gets or sets if this controller is enabled.
        /// </summary>
        public bool IsEnabled {
            get {
                return _isEnabled;
            }
            set {
                _isEnabled = value;
            }
        }

        /// <summary>
        /// Gets or sets the scale distance. Smaller values mean larger scale.
        /// </summary>
        public float ScaleDistance {
            get {
                return _scaleDistance;
            }
            set {
                _scaleDistance = value;
            }
        }

        /// <summary>
        /// Creates a new constant size controller.
        /// </summary>
        /// <param name="cam"></param>
        /// <param name="scaleDistance"></param>
        public ConstantSizeController(Camera cam, float scaleDistance) {
            _camera = cam;
            _scaleDistance = scaleDistance;
            _isEnabled = true;
        }

        /// <summary>
        /// Update the controller.
        /// </summary>
        /// <param name="time">Interpolation time</param>
        /// <param name="caller">Spatial this controller is attached to</param>
        public void Update(GameTime time, Spatial caller) {
            if(_isEnabled) {
                Vector3 camLoc = _camera.Position;
                float dist = Vector3.Distance(camLoc, caller.WorldTranslation);
                float scale = dist / _scaleDistance;
                caller.SetScale(scale);
            }
        }
    }
}
